package com.itheima.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.annotation.Log;
import com.itheima.tlias.dto.SelectClazzDTO;
import com.itheima.tlias.mapper.ClazzMapper;
import com.itheima.tlias.po.InsertClazzParam;
import com.itheima.tlias.po.SelectClazzPageParam;
import com.itheima.tlias.po.UpdateClazzParam;
import com.itheima.tlias.service.ClazzService;
import com.itheima.tlias.util.ResultUtil;
import com.itheima.tlias.vo.PageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {

    @Resource
    ClazzMapper clazzMapper;

    @Override
    public ResultUtil selectClazzByPage(SelectClazzPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        //分页插件
        List<SelectClazzDTO> dto = clazzMapper.selectClazzByPage(param);
        PageVO<SelectClazzDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        //返回总页面的数量
        pageVO.setRows(dto);
        //返回当前页面的数据
        return ResultUtil.success(pageVO);
    }

    @Log
    @Override
    public int insertClazz(InsertClazzParam param) {
        return clazzMapper.insertClazz(param);
    }

    @Override
    public SelectClazzDTO selectClazzById(Integer id) {
        return clazzMapper.selectClazzById(id);
    }

    @Log
    @Override
    public int deleteClazzById(Integer id) {
        return clazzMapper.deleteClazzById(id);
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateClazz(UpdateClazzParam param) {
        param.setUpdateTime(LocalDateTime.now());
        return clazzMapper.updateClazz(param);
    }

    @Override
    public List<SelectClazzDTO> selectClazzAll() {
        return clazzMapper.selectClazzAll();
    }
}
